package week6.day40.title31;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 1.将一个字符串，转换为Date。将一个Date转换为 2023-10-23 10:01:02这种格式。
 * 2.创建一个学生数组，学生数组中有生日，要求类型是Date。创建一组学生，并且放入生日。
 * 创建4个学生，并且放入。
 * zs 18 hubei "2023-10-02"
 * ls 17 beijing "2022-11-02"
 * wu 19 hubei "2021-09-02"
 * zl 21 tianjin "2009-10-02"
 * @Author fengzihao
 * @Date 2024-05-24 20:05
 */
public class DateTest {
    public static void main(String[] args) throws ParseException {
        String time = "2023-10-23 10:01:02";
        Date date = getDateByString1(time);
        System.out.println("date = " + getStringByDate1(date));
        Date date2 = new Date();
        Date date1 = getFormatDateByDate(date2);
        System.out.println("date1 = " + getStringByDate1(date1));
        Student[] students = generateStudents();
        for (Student student : students) {
            System.out.println("student = " + student);
        }
    }

    public static Student[] generateStudents() throws ParseException {
        Student[] students = new Student[4];
        students[0] = new Student("zs",18,"hubei","2023-10-02");
        students[1] = new Student("ls",17,"beijing","2022-11-02");
        students[2] = new Student("wu",19,"hubei","2021-09-02");
        students[3] = new Student("zl",21,"tianjin","2009-10-02");
        return students;
    }

    public static Date getFormatDateByDate(Date date) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format(date);
        return getDateByString1(time);
    }

    public static String getStringByDate1(Date date){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    public static String getStringByDate2(Date date){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    public static Date getDateByString1(String time) throws ParseException {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return dateFormat.parse(time);
    }
    public static Date getDateByString2(String time) throws ParseException {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.parse(time);
    }
}
class Student{
    String name;
    int age;
    String address;
    Date birthday;

    public Student() {
    }

    public Student(String name, int age, String address, String birthday) throws ParseException {
        this.name = name;
        this.age = age;
        this.address = address;
        this.birthday = DateTest.getDateByString2(birthday);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                ", birthday=" + DateTest.getStringByDate2(birthday) +
                '}';
    }
}
